# src/db/create_tables.py
import pymysql
from .config import DB_CONFIG  # 修正导入路径

def create_database():
    # 先连接到 MySQL 服务器（不指定数据库）
    conn = pymysql.connect(
        host=DB_CONFIG['host'],
        user=DB_CONFIG['user'],
        password=DB_CONFIG['password'],
        port=DB_CONFIG['port']
    )
    
    with conn.cursor() as cursor:
        # 创建数据库（如果不存在）
        cursor.execute("CREATE DATABASE IF NOT EXISTS art_trading CHARACTER SET utf8mb4;")
        cursor.execute("USE art_trading;")
        
        # 创建用户表（补充完整字段）
        cursor.execute("""
            CREATE TABLE User (
                user_id INT AUTO_INCREMENT PRIMARY KEY,
                username VARCHAR(20) UNIQUE NOT NULL,
                password VARCHAR(100) NOT NULL,
                real_name VARCHAR(100) NOT NULL,
                contact_info VARCHAR(100) NOT NULL,
                address VARCHAR(500) NOT NULL,
                user_role ENUM('买家', '卖家') NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
        """)
        
        # 可继续添加其他表（如 Artist、Artwork）
        print("表结构创建成功！")
    
    conn.commit()
    conn.close()

if __name__ == "__main__":
    create_database()
